package com.offcn.user.controller;

import com.offcn.user.pojo.User;
import com.offcn.user.pojo.UserPhone;
import com.offcn.user.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

/**
 * @ClassName: UserController
 * @Description:
 * @Date: 2020/11/10 16:38
 */
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;
    @PostMapping("/add")
    public ModelAndView addUser(User user){
        userService.addUser(user);
        ModelAndView mv=new ModelAndView("success");
        mv.addObject("user", user);
        return mv;
    }
    /**
     * 用户发送验证码
     */
    @GetMapping("/sendCode")
    public Map<String,String> sendCode(@RequestParam String phone){
        String code=(long) (Math.random()*1000000)+"";
        //System.out.println(code);
       Map<String,String> map=new HashMap<>();
       map.put("phone", phone);
       map.put("code", code);
       return map;
    }
    /**
     * 用户注册
     */
    @PostMapping("/regirest")
    public ModelAndView regirest(UserPhone phone){
        userService.addPhone(phone);
        ModelAndView mv=new ModelAndView("login");
        mv.addObject("phone", phone);
        return mv;
    }
}
